
package de.swta.freezay.database.wicketAdapter;

import de.swta.freezay.database.JPAController;
import de.swta.freezay.database.dbEntities.Unit;
import org.apache.wicket.model.LoadableDetachableModel;

/**
 * A LoadableDetachableModel for unit entities
 *
 * @author Christian Baeumer
 * @see LoadableDetachableModel
 */
public class DetachableUnitModel extends LoadableDetachableModel<Unit> {

    /** the unit id for this unit model */
    private int unitID;

    public DetachableUnitModel(final Unit u) {
        this(u.getId());
    }


    public DetachableUnitModel(final int id) {
        unitID = id;
    }

    @Override
    protected Unit load() {
        /** loads item from database */
        return JPAController.UnitJpaController.findUnit(unitID);
    }



    @Override
    public int hashCode() {
        int hash = 3;
        hash = 47 * hash + unitID;
        return hash;
    }

    /**
     * used for dataview with ReuseIfModelsEqualStrategy item reuse strategy
     *
     * @see org.apache.wicket.markup.repeater.ReuseIfModelsEqualStrategy
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj)
    {
        if (obj == null) {
            return false;
        } else if (obj == this) {
            return true;
        } else if (obj instanceof DetachableItemModel) {
            DetachableUnitModel other = (DetachableUnitModel) obj;
            return other.unitID == unitID;
        }

        return false;
    }

}
